import { useUserStore } from './../stores/modules/user'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

/**
 * vue2 创建router实例 new VueRouter
 * vue3 创建router实例 createRouter
 *
 *          history   hash
 * 带#      不带       带
 * 后端支持  需要       不需要
 *
 * history模式 createWebHistory
 * hash模式 createWebHashHistory
 */

// console.log(import.meta.env.BASE_URL)
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  /**
   * 配置一级路由
   * 1. 创建页面组件 playground页面
   * 2. 路径 组件 配置规则
   * 3. 路由出口
   */
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录页' }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        {
          path: 'user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '用户中心' }
        },
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '消息通知' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      // beforeEnter是路由配置规则的时候的属性 - 进入页面前触发
      beforeEnter(to) {
        // 什么都不写或者return true 代表放行
        // return false不放行
        // return '/login' return 地址 拦截到地址
        // 支付失败跳到问诊室，让用户调回问诊记录 /user/consult 直接写enter
        if (to.query.payResult === 'false') {
          return '/user/consult'
        }
        // 啥都不写 放行
      }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    },
    {
      path: '/playground',
      component: () => import('@/views/Playground/index.vue'),
      children: [
        {
          path: '/playground-a',
          component: () => import('@/views/Playground/PlaygroundA.vue')
        },
        {
          path: '/playground-b',
          component: () => import('@/views/Playground/PlaygroundB.vue')
        },
        {
          path: '/playground-c',
          component: () => import('@/views/Playground/PlaygroundC.vue')
        }
      ]
    }
  ]
})

/**
 *                vuerouter3(vue2)   vuerouter4(vue3)
 *  拦截到一个页面    next('/login')     return '/login'
 *  放行            next()             不返回或者return true
 *  第三个参数next   有，拦截和放行都要用   可选，可用可不用（建议不用）
 */

// token拦截
// 基本所有的页面都要登录才能访问(是否登录看有没有token)
// 没有登录且不在白名单 就拦截到登录页 /login
const whiteList = ['/login', '/login/callback']
router.beforeEach((to) => {
  NProgress.start()
  // 组件外的store要写在路由守卫里面
  const userStore = useUserStore()
  if (!userStore.user?.token && !whiteList.includes(to.path)) return '/login'
  // 代码不走if 往下走就是放行
})
router.afterEach((to) => {
  NProgress.done()
  // console.log(to.meta.title)
  document.title = to.meta.title || '优医问诊'
})
export default router
